<?php


namespace App\Controller\App;


use App\Base\BaseController4App;
use App\Entity\OrderCancel;
use App\Entity\Orders;
use App\Entity\User;
use App\Repository\BannerRepository;
use App\Repository\ConfigRepository;
use App\Repository\MoneyLogTypeRepository;
use App\Repository\MoneyRepository;
use App\Repository\MoneyTypeRepository;
use App\Repository\OrdersRepository;
use App\Repository\SalesCutRepository;
use App\Repository\SalesUserJuanRepository;
use App\Repository\SiteGunRepository;
use App\Repository\SiteOilCategoryRepository;
use App\Repository\SiteOilRepository;
use App\Repository\SiteRepository;
use App\Repository\UserRepository;
use App\Service\MoneyService;
use App\Service\PayService;
use Symfony\Component\Routing\Annotation\Route;

/**
 * @Route("/app/pay")
 */
class AppPayController extends BaseController4App
{
    /**
     * @Route("/charge", name="AppPayCharge")
     */
    public function AppPayCharge(
        SiteOilRepository $siteOilRepository
        , PayService $payService
        , ConfigRepository $configRepository
        ,MoneyLogTypeRepository $moneyLogTypeRepository
        ,MoneyTypeRepository $moneyTypeRepository
    )
    {
        $user = $this->needToBeUser();
        $data = [];
        $req = $this->getRequestData();
        $money = $req["money"] ?? "";
        $charge = $configRepository->getKVNumArray("CHARGE");
//        $moneys = array_keys($charge);
        $array = [];
        foreach ($charge as $row) {
            $array[] = $row[0];
        }

        if (!in_array($money,$array)) {
            return $this->json_out(0, "支付金额不合法",[$charge,$array]);
        }
        $payReal = $money;

        //根据情况发起支付
        $param = ["info"=>"充值订单"];
        $moneyLogTypeRepository->findOneBy(array("code"=>101));
        $payOrder = $payService->createPayOrder($user, $payReal,$moneyTypeRepository->find(MoneyService::__MONEY_XIAN_JIN__),$param);
        $openid = $user->getOpenid();
//        $payReal = 0.02;
        $data = $payService->createPayParam($payReal, 100, $payOrder->getSn(), "charge", $openid);
        return $this->json_out(1, "成功发起请求", $data);
    }

}